﻿using System;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using TeachingManageSystem.BLL;
using TeachingManageSystem.Entity;

namespace TeachingManageSystem.Web.Warehouse
{
    public partial class BookWarehousing : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                DrpBookType.DataSource = BookTypeBll.GetBookType();
                DrpBookType.DataBind();
            }
           
        }

        protected void Button1Click(object sender, EventArgs e)
        {
            //获取数据
            var getName = FileUpload1.PostedFile.FileName;//获取图片名

            //上传文件
            var getLastpath = FileUploadCompant(FileUpload1);
            //获取上传文件流
            var getbyte = new byte[FileUpload1.PostedFile.ContentLength];
            var filestream = FileUpload1.PostedFile.InputStream;
            //读取数据
            filestream.Read(getbyte, 0, FileUpload1.PostedFile.ContentLength);

            //插入数据
            var pic = new Picture
                          {
                              PicturenName = getName,
                              Picturn = getbyte,
                              PictureType = FileUpload1.PostedFile.ContentType,
                              PictureSize = FileUpload1.PostedFile.ContentLength,
                              PicturePath = getLastpath
                          };
            PictureBll.InsertBookImage(pic);
            Session["Image"] = pic;
            img.Src = "ShowImage.aspx";
        }

       /// <summary>
        /// 文件上传操作
       /// </summary>
       /// <param name="getfileupload"></param>
       /// <returns></returns>
        public string FileUploadCompant(FileUpload getfileupload)
        {
            var takeServerpath = string.Empty;   //保存到服务器路径
            try
            {
                if(string.IsNullOrEmpty(getfileupload.PostedFile.FileName))
                {
                    ScriptManager.RegisterStartupScript(Page, GetType(), "aler ", "alert( '请选择上传文件的路径!'); ", true);
                    getfileupload.Focus();
                }
                else
                {
                    string filepath = getfileupload.PostedFile.FileName;//获得全路径
                    //获得文件名
                    string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);

                    //保存服务器路径 -唯一文件名
                    string serverpath = Server.MapPath(@"FileuploadDict/") + DateTime.Now.ToString("yyy-MM-dd-hh-mm-ss") + Session.SessionID + filename;

                    //保存到服务器上
                    getfileupload.SaveAs(serverpath);

                    //保存成功，提示信息
                    ScriptManager.RegisterStartupScript(Page, GetType(), "aler ", "alert( '文件上传成功！'); ", true);
                    takeServerpath = serverpath;
                }
            }
            catch (Exception se)
            {
                //抓取一场信息 并提示
                ScriptManager.RegisterStartupScript(Page, GetType(), "aler ", "alert( '文件上传失败-上传过程出现如下异常信息:" + se.Message.ToString(CultureInfo.InvariantCulture) + "！'); ", true);
            }
            return takeServerpath;
        }

        protected void Button2Click(object sender, EventArgs e)
        {
            var book = new Book();
            book.Name = TbxBookName.Text;
            book.Number = TbxBookNumber.Text;
            book.Publisher = TbxBookPublished.Text;
            book.PublistedTime=Convert.ToDateTime(TbxBookPublishedTime.Text);
            book.Author = TbxBookAuthor.Text;
            book.Price = Convert.ToDecimal(TbxBookPrice.Text);
            book.BookTypeID = Convert.ToInt32(BookBll.GetBookTypeIdByName(DrpBookType.SelectedValue));
            //book.BookTypeID = 249;
            book.Remark = TbxRemark.Text;
            BookBll.InsertBook(book);

            TbxBookNumber.Text = "";
            TbxBookNumber.Text = "";
            TbxBookPublished.Text="";
            TbxBookPublishedTime.Text = "";
            TbxBookAuthor.Text="";
            TbxBookPrice.Text = "";
            Response.Redirect("ShowBook.aspx");

        }

       
    }
}